package com.jackrain.nea.fc.dl.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSONObject;
import com.jackrain.nea.exception.NDSException;
import com.jackrain.nea.fc.dl.api.*;
import com.jackrain.nea.util.ApplicationContextHandle;
import com.jackrain.nea.util.ReferenceUtil;
import com.jackrain.nea.util.ValueHolder;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.HashMap;

/**
 * @author xuxinshuai
 * @date 2018/05/11
 **/
@Api(description = "WMS")
@RestController
public class ViewReloadDataCtrl {


    @Reference(version = "1.0", group = "dl")
    private ViewReLoadDataCmd viewReLoadDataCmd;

    @Reference(version = "1.0", group = "dl")
    private ViewReLoadTranOutDataCmd viewReLoadTranOutDataCmd;

    @Reference(version = "1.0", group = "dl")
    private ViewReloadPurInCmd viewReloadPurInCmd;

    @Reference(version = "1.0", group = "dl")
    private ViewReloadPurInOneCmd viewReloadPurInOneCmd;

    @Reference(version = "1.0", group = "dl")
    private ViewReLoadPurRetDataCmd viewReLoadPurRetDataCmd;

    @Reference(version = "1.0", group = "dl")
    private ViewReloadTranOutSendCmd viewReloadTranOutSendCmd;

    @Reference(version = "1.0", group = "dl")
    private ViewReloadTranInAcceptCmd viewReloadTranInAcceptCmd;

    @ApiOperation(value = "定时拉去数据-查询")
    @RequestMapping(path = "/fc/api/wmsreload/data", method = RequestMethod.POST)
    public JSONObject queryin(HttpServletRequest request,
                              @RequestParam(value = "param", required = false) String param) throws Exception {

        ValueHolder result = new ValueHolder();
        try {
            HashMap map = new HashMap(16);
            map.put("param", param);
            Object o = ReferenceUtil.refer(ApplicationContextHandle.getApplicationContext(), "com.jackrain.nea.fc.dl.api.ViewReLoadDataCmd",
                    "dl", "1.0");
            result = ((ViewReLoadDataCmd) o).execute(map);
            if (result == null) {
                result = new ValueHolder();
                result.put("code", -1);
                result.put("error", "服务错误：返回为空");
            }
            if (result.get("code") == null) {
                result.put("code", 0);
            }

        } catch (Exception e) {
            result.put("code", -1);
            if (e instanceof NDSException) {
                result.put("error", "服务错误：" + e.getMessage());
            } else {
                StringWriter sw = new StringWriter();
                PrintWriter pw = new PrintWriter(sw);
                e.printStackTrace(pw);
                result.put("error", "服务错误：" + sw.toString());
            }
        }
        return result.toJSONObject();
    }

    @ApiOperation(value = "定时拉取调拨出库数据-查询")
    @RequestMapping(path = "/fc/api/wmsreload/outdata", method = RequestMethod.POST)
    public JSONObject queryout(HttpServletRequest request,
                              @RequestParam(value = "param", required = false) String param) throws Exception {

        ValueHolder result = new ValueHolder();
        try {
            HashMap map = new HashMap(16);
            map.put("param", param);
            Object o = ReferenceUtil.refer(ApplicationContextHandle.getApplicationContext(), "com.jackrain.nea.fc.dl.api.ViewReLoadTranOutDataCmd",
                    "dl", "1.0");
            result = ((ViewReLoadTranOutDataCmd) o).execute(map);
            if (result == null) {
                result = new ValueHolder();
                result.put("code", -1);
                result.put("error", "服务错误：返回为空");
            }
            if (result.get("code") == null) {
                result.put("code", 0);
            }

        } catch (Exception e) {
            result.put("code", -1);
            if (e instanceof NDSException) {
                result.put("error", "服务错误：" + e.getMessage());
            } else {
                StringWriter sw = new StringWriter();
                PrintWriter pw = new PrintWriter(sw);
                e.printStackTrace(pw);
                result.put("error", "服务错误：" + sw.toString());
            }
        }
        return result.toJSONObject();
    }

    @ApiOperation(value = "定时拉采购订单数据-查询")
    @RequestMapping(path = "/fc/api/wmsreload/purindata", method = RequestMethod.POST)
    public JSONObject querypurin(HttpServletRequest request,
                               @RequestParam(value = "param", required = false) String param) throws Exception {

        ValueHolder result = new ValueHolder();
        try {
            HashMap map = new HashMap(16);
            map.put("param", param);
            Object o = ReferenceUtil.refer(ApplicationContextHandle.getApplicationContext(), "com.jackrain.nea.fc.dl.api.ViewReloadPurInCmd",
                    "dl", "1.0");
            result = ((ViewReloadPurInCmd) o).execute(map);
            if (result == null) {
                result = new ValueHolder();
                result.put("code", -1);
                result.put("error", "服务错误：返回为空");
            }
            if (result.get("code") == null) {
                result.put("code", 0);
            }

        } catch (Exception e) {
            result.put("code", -1);
            if (e instanceof NDSException) {
                result.put("error", "服务错误：" + e.getMessage());
            } else {
                StringWriter sw = new StringWriter();
                PrintWriter pw = new PrintWriter(sw);
                e.printStackTrace(pw);
                result.put("error", "服务错误：" + sw.toString());
            }
        }
        return result.toJSONObject();
    }

    @ApiOperation(value = "定时拉取采购退货数据-查询")
    @RequestMapping(path = "/fc/api/wmsreload/purretdata", method = RequestMethod.POST)
    public JSONObject querypurret(HttpServletRequest request,
                               @RequestParam(value = "param", required = false) String param) throws Exception {

        ValueHolder result = new ValueHolder();
        try {
            HashMap map = new HashMap(16);
            map.put("param", param);
            Object o = ReferenceUtil.refer(ApplicationContextHandle.getApplicationContext(), "com.jackrain.nea.fc.dl.api.ViewReLoadPurRetDataCmd",
                    "dl", "1.0");
            result = ((ViewReLoadPurRetDataCmd) o).execute(map);
            if (result == null) {
                result = new ValueHolder();
                result.put("code", -1);
                result.put("error", "服务错误：返回为空");
            }
            if (result.get("code") == null) {
                result.put("code", 0);
            }

        } catch (Exception e) {
            result.put("code", -1);
            if (e instanceof NDSException) {
                result.put("error", "服务错误：" + e.getMessage());
            } else {
                StringWriter sw = new StringWriter();
                PrintWriter pw = new PrintWriter(sw);
                e.printStackTrace(pw);
                result.put("error", "服务错误：" + sw.toString());
            }
        }
        return result.toJSONObject();
    }

    @ApiOperation(value = "定时拉采购入库数据-查询 验收状态")
    @RequestMapping(path = "/fc/api/wmsreload/purindata1", method = RequestMethod.POST)
    public JSONObject querypurin1(HttpServletRequest request,
                                 @RequestParam(value = "param", required = false) String param) throws Exception {

        ValueHolder result = new ValueHolder();
        try {
            HashMap map = new HashMap(16);
            map.put("param", param);
            Object o = ReferenceUtil.refer(ApplicationContextHandle.getApplicationContext(), "com.jackrain.nea.fc.dl.api.ViewReloadPurInOneCmd",
                    "dl", "1.0");
            result = ((ViewReloadPurInOneCmd) o).execute(map);
            if (result == null) {
                result = new ValueHolder();
                result.put("code", -1);
                result.put("error", "服务错误：返回为空");
            }
            if (result.get("code") == null) {
                result.put("code", 0);
            }

        } catch (Exception e) {
            result.put("code", -1);
            if (e instanceof NDSException) {
                result.put("error", "服务错误：" + e.getMessage());
            } else {
                StringWriter sw = new StringWriter();
                PrintWriter pw = new PrintWriter(sw);
                e.printStackTrace(pw);
                result.put("error", "服务错误：" + sw.toString());
            }
        }
        return result.toJSONObject();
    }

    @ApiOperation(value = "调拨入库 验收状态")
    @RequestMapping(path = "/fc/api/wmsreload/traninaccept", method = RequestMethod.POST)
    public JSONObject querytraninaccept(HttpServletRequest request,
                                  @RequestParam(value = "param", required = false) String param) throws Exception {

        ValueHolder result = new ValueHolder();
        try {
            HashMap map = new HashMap(16);
            map.put("param", param);
            Object o = ReferenceUtil.refer(ApplicationContextHandle.getApplicationContext(), "com.jackrain.nea.fc.dl.api.ViewReloadTranInAcceptCmd",
                    "dl", "1.0");
            result = ((ViewReloadTranInAcceptCmd) o).execute(map);
            if (result == null) {
                result = new ValueHolder();
                result.put("code", -1);
                result.put("error", "服务错误：返回为空");
            }
            if (result.get("code") == null) {
                result.put("code", 0);
            }

        } catch (Exception e) {
            result.put("code", -1);
            if (e instanceof NDSException) {
                result.put("error", "服务错误：" + e.getMessage());
            } else {
                StringWriter sw = new StringWriter();
                PrintWriter pw = new PrintWriter(sw);
                e.printStackTrace(pw);
                result.put("error", "服务错误：" + sw.toString());
            }
        }
        return result.toJSONObject();
    }

    @ApiOperation(value = "调拨出库 发出状态")
    @RequestMapping(path = "/fc/api/wmsreload/tranoutsend", method = RequestMethod.POST)
    public JSONObject querytranoutsend(HttpServletRequest request,
                                  @RequestParam(value = "param", required = false) String param) throws Exception {

        ValueHolder result = new ValueHolder();
        try {
            HashMap map = new HashMap(16);
            map.put("param", param);
            Object o = ReferenceUtil.refer(ApplicationContextHandle.getApplicationContext(), "com.jackrain.nea.fc.dl.api.ViewReloadTranOutSendCmd",
                    "dl", "1.0");
            result = ((ViewReloadTranOutSendCmd) o).execute(map);
            if (result == null) {
                result = new ValueHolder();
                result.put("code", -1);
                result.put("error", "服务错误：返回为空");
            }
            if (result.get("code") == null) {
                result.put("code", 0);
            }

        } catch (Exception e) {
            result.put("code", -1);
            if (e instanceof NDSException) {
                result.put("error", "服务错误：" + e.getMessage());
            } else {
                StringWriter sw = new StringWriter();
                PrintWriter pw = new PrintWriter(sw);
                e.printStackTrace(pw);
                result.put("error", "服务错误：" + sw.toString());
            }
        }
        return result.toJSONObject();
    }



}
